<template>
  <el-container class="home-container">
    <el-header>
      <div class="logo-box">
        <img src="../assets/img/heima.png" alt>
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '65px' : '200px'">
        <el-row>
          <el-col>
            <div
              style="background-color:#4A5064;height:25px;font-size:12px;text-align:center;line-height:25px;letter-spacing:0.1em;color:white;cursor:pointer;"
              @click="isCollapse=!isCollapse"
            >|||</div>
            <el-menu
              background-color="#333744"
              text-color="#fff"
              active-text-color="#409EFF"
              :unique-opened="true"
              :collapse="isCollapse"
              :collapse-transition="false"
              style="width:200px"
              :router="true"
            >
              <el-submenu :index="item.id+''" v-for="(item,k) in menuList" :key="item.id">
                <template slot="title">
                  <i :class="'iconfont icon-'+iconList[k]"></i>
                  <span>{{ item.authName }}</span>
                </template>
                <el-menu-item
                  :index="item2.path"
                  v-for="item2 in item.children"
                  :key="item2.id"
                >
                  <i class="el-icon-menu"></i>
                  <span>{{ item2.authName }}</span>
                </el-menu-item>
              </el-submenu>
            </el-menu>
          </el-col>
        </el-row>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList()
  },
  data() {
    return {
      menuList: [],
      iconList: ['users', 'tijikongjian', 'shangpin', 'danju', 'baobiao'],
      isCollapse: false
    }
  },
  methods: {
    logout() {
      this.$confirm('确认要退出系统?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          window.sessionStorage.removeItem('token')
          this.$router.push('/login')
        })
        .catch(() => {})
    },
    // 获得左侧列表数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        return this.$message.error('获取权限菜单失败')
      }

      this.menuList = res.data
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
  .el-header {
    background-color: rgb(55, 61, 65);
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0;
    padding-right: 20px;
    .logo-box {
      font-size: 22px;
      color: #fff;
      display: flex;
      align-items: center;
      user-select: none;
      img {
        width: 50px;
        height: 50px;
        margin-right: 10px;
      }
    }
  }
}
.el-aside {
  background-color: rgb(51, 55, 68);
}

.el-main {
  background-color: rgb(234, 237, 241);
}
</style>
